package com.irdstudio.bsp.executor.core.plugin.dataload;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据表操作类-[表名: 01.数据装载配置表(plugin_load_conf)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-04-29
 */
public class PluginLoadConfDao {

	private static final Logger logger = LoggerFactory.getLogger(PluginLoadConfDao.class);
	/* 连接对象 */
	Connection conn = null;	

	public PluginLoadConfDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public PluginLoadConf queryWithKeys(String pluginConfId,String tableName)
			throws SQLException {
		PluginLoadConf dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_load_conf WHERE plugin_conf_id=? and table_name=?");
			ps.setString(1,pluginConfId);
			ps.setString(2,tableName);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new PluginLoadConf();
				dc.setPluginConfId(rs.getString("plugin_conf_id"));
				dc.setConfSort(rs.getInt("conf_sort"));
				dc.setTableName(rs.getString("table_name"));
				dc.setTableCnname(rs.getString("table_cnname"));
				dc.setTableType(rs.getString("table_type"));
				dc.setUpSysname(rs.getString("up_sysname"));
				dc.setTableLoadMode(rs.getString("table_load_mode"));
				dc.setLoadFromFile(rs.getString("load_from_file"));
				dc.setFileCharset(rs.getString("file_charset"));
				dc.setLoadBufferSize(rs.getInt("load_buffer_size"));
				dc.setLoadSeparator(rs.getString("load_separator"));
				dc.setLoadFields(rs.getString("load_fields"));
				dc.setCreateTableDdl(rs.getString("create_table_ddl"));
				dc.setBeforeLoadSql(rs.getString("before_load_sql"));
				dc.setAfterLoadSql(rs.getString("after_load_sql"));
				dc.setFileRowFlag(rs.getString("file_row_flag"));
				dc.setLoadWarnFlag(rs.getString("load_warn_flag"));
				dc.setDiffCompMethod(rs.getString("diff_comp_method"));
				dc.setLimitPercent(rs.getInt("limit_percent"));
				dc.setDiffDealMethod(rs.getString("diff_deal_method"));
				dc.setLoadFaildDeal(rs.getString("load_faild_deal"));
				dc.setLastModifyDate(rs.getString("last_modify_date"));
				dc.setIsDealEmptyStr(rs.getString("is_deal_empty_str"));
				dc.setDealEmptyStr(rs.getString("deal_empty_str"));
				dc.setIsFirstline(rs.getString("is_first_line"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginLoadConfWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}
	
	/**
	 * @description:使用pluginConfId查询
	 * @param pluginConfId
	 * @return
	 * @throws SQLException
	 * @date:2018年2月19日 下午3:07:32
	 * @author:hengyh
	 */
	public List<PluginLoadConf> queryWithPluginConfId(String pluginConfId)
			throws SQLException {
		List<PluginLoadConf> dcList = new ArrayList<PluginLoadConf>();
		PluginLoadConf dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_load_conf WHERE plugin_conf_id=? order by conf_sort");
			ps.setString(1,pluginConfId);
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new PluginLoadConf();
				dc.setPluginConfId(rs.getString("plugin_conf_id"));
				dc.setConfSort(rs.getInt("conf_sort"));
				dc.setTableName(rs.getString("table_name"));
				dc.setTableCnname(rs.getString("table_cnname"));
				dc.setTableType(rs.getString("table_type"));
				dc.setUpSysname(rs.getString("up_sysname"));
				dc.setTableLoadMode(rs.getString("table_load_mode"));
				dc.setLoadFromFile(rs.getString("load_from_file"));
				dc.setFileCharset(rs.getString("file_charset"));
				dc.setLoadBufferSize(rs.getInt("load_buffer_size"));
				dc.setLoadSeparator(rs.getString("load_separator"));
				dc.setLoadFields(rs.getString("load_fields"));
				dc.setCreateTableDdl(rs.getString("create_table_ddl"));
				dc.setBeforeLoadSql(rs.getString("before_load_sql"));
				dc.setAfterLoadSql(rs.getString("after_load_sql"));
				dc.setFileRowFlag(rs.getString("file_row_flag"));
				dc.setLoadWarnFlag(rs.getString("load_warn_flag"));
				dc.setDiffCompMethod(rs.getString("diff_comp_method"));
				dc.setLimitPercent(rs.getInt("limit_percent"));
				dc.setDiffDealMethod(rs.getString("diff_deal_method"));
				dc.setLoadFaildDeal(rs.getString("load_faild_deal"));
				dc.setLastModifyDate(rs.getString("last_modify_date"));
				dc.setIsDealEmptyStr(rs.getString("is_deal_empty_str"));
				dc.setDealEmptyStr(rs.getString("deal_empty_str"));
				dc.setIsFirstline(rs.getString("is_first_line"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginLoadConfWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
//	public List<PluginLoadConf> queryWithCond(String szCondition,String szOrderBy)
//			throws SQLException {
//		List<PluginLoadConf> dcList = new ArrayList<PluginLoadConf>();
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			ps = conn.prepareStatement("SELECT * FROM plugin_load_conf "
//					+ szCondition + " " + szOrderBy);
//			rs = ps.executeQuery();
//			while (rs.next()) {
//				PluginLoadConf dc = new PluginLoadConf();
//				dc.setPluginConfId(rs.getString("plugin_conf_id"));
//				dc.setConfSort(rs.getInt("conf_sort"));
//				dc.setTableName(rs.getString("table_name"));
//				dc.setTableCnname(rs.getString("table_cnname"));
//				dc.setTableType(rs.getString("table_type"));
//				dc.setUpSysname(rs.getString("up_sysname"));
//				dc.setTableLoadMode(rs.getString("table_load_mode"));
//				dc.setLoadFromFile(rs.getString("load_from_file"));
//				dc.setFileCharset(rs.getString("file_charset"));
//				dc.setLoadBufferSize(rs.getInt("load_buffer_size"));
//				dc.setLoadSeparator(rs.getString("load_separator"));
//				dc.setLoadFields(rs.getString("load_fields"));
//				dc.setCreateTableDdl(rs.getString("create_table_ddl"));
//				dc.setBeforeLoadSql(rs.getString("before_load_sql"));
//				dc.setAfterLoadSql(rs.getString("after_load_sql"));
//				dc.setFileRowFlag(rs.getString("file_row_flag"));
//				dc.setLoadWarnFlag(rs.getString("load_warn_flag"));
//				dc.setDiffCompMethod(rs.getString("diff_comp_method"));
//				dc.setLimitPercent(rs.getInt("limit_percent"));
//				dc.setDiffDealMethod(rs.getString("diff_deal_method"));
//				dc.setLoadFaildDeal(rs.getString("load_faild_deal"));
//				dc.setLastModifyDate(rs.getString("last_modify_date"));
//				dc.setIsDealEmptyStr(rs.getString("is_deal_empty_str"));
//				dc.setDealEmptyStr(rs.getString("deal_empty_str"));
//				dc.setIsFirstline(rs.getString("is_first_line"));
//				dcList.add(dc);
//			}
//		} catch (SQLException e) {
//			throw new SQLException("queryPluginLoadConfWithCond is Wrong!"
//					+ e.getMessage());
//		} finally {
//			close(rs, null, ps);
//		}
//		return dcList;
//	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

}
